package com.hz.control;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import com.hz.dbconn.DBConnect;
import com.hz.model.Comment;

public class CommentControl {
    //add comment
    public static boolean addComment(Comment comment){
	DBConnect dbc = new DBConnect();
	String sql = "insert into comment(userId,title,content,addtime)values(?,?,?,?)";
	dbc.prepareStatement(sql);
	try{
	       int index = 1;
	       dbc.setInt(index++, comment.getUserId());
	       dbc.setString(index++, comment.getTitle());
	       dbc.setString(index++, comment.getContent());
	       dbc.setString(index++,comment.getAddTime());
		dbc.executeUpdate();
		return true;
	}catch(SQLException e){
		e.printStackTrace();
		System.out.println("查询数据库，插入留言失败!");
		return false;
	}finally{
		dbc.close();
	}
}
    
   //chazhao
    public static Comment getComment (int id) {
    	DBConnect dbc = new DBConnect();
    	String sql = "select * from comment where id='"+id+"'";
    	ResultSet rs = null;
    	Comment c =null;
    	dbc.prepareStatement(sql);
    	try {
    		rs = dbc.executeQuery();
			if(rs.next()){
				c = new Comment();
				c.setAddTime(rs.getString("addtime"));
				c.setContent(rs.getString("content"));
				c.setId(rs.getInt("id"));
				c.setTitle(rs.getString("title"));
				c.setUserId(rs.getInt("userId"));
			}
			return c;
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			dbc.close();
		}
		return null;
	}
  //delete comment
	public static boolean deleteComment(int id) {
		DBConnect dbc = new DBConnect();
		String sql = "delete from comment where id=?";
		dbc.prepareStatement(sql);
		try{
			dbc.setInt(1, id);
			dbc.executeUpdate();
			return true;
		}catch(SQLException e){
			e.printStackTrace();
			return false;
		}finally{
			dbc.close();
		}
	}
	//collection comment
	public static Collection<Comment> getAllComment() {
		DBConnect dbc = new DBConnect();
		String sql = "select id,title,content,addtime,userId from comment order by id desc";
		Collection<Comment> commentlist = new ArrayList<Comment>();
		ResultSet rs = null;
		dbc.prepareStatement(sql);
		try{
			rs = dbc.executeQuery();
			while(rs.next()){
				Comment comment = new Comment();
				comment.setId(rs.getInt("id"));
				comment.setTitle(rs.getString("title"));
				comment.setContent(rs.getString("content"));
				comment.setAddTime(rs.getString("addtime"));
				comment.setUserId(rs.getInt("userId"));
				commentlist.add(comment);
			}
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("Collection mistake!");
			return null;
		}finally{
			dbc.close();
		}
		return commentlist;
	}
	//test method
	public static void main(String[] args){
		Comment comment = new Comment();
		comment.setContent("content");comment.setTitle("title");comment.setAddTime("2012-4-10");
		CommentControl.addComment(comment);
		//CommentControl.deleteComment(2);
		
	}

}
